import csv
import json
import os
from collections import Counter
dicts={}
with open('D:\\Work\\Tool\\tiktok\\TikToks\\Hashtag_foodtiktok_2020-11-16_2.json', 'r', encoding='utf-8', newline='\n') as filename_input:
    i = 0
    for line in filename_input:
        z = json.loads(line)
        if 'authorInfos' in z.keys():
            if z['authorInfos']['uniqueId'] not in dicts.keys():
                dicts[z['authorInfos']['uniqueId']]={'in':[z['itemInfos']['id']],'out':[],'vc':int(z['authorStats']['videoCount']),'fc':int(z['authorStats']['followerCount'])}
            else:
                if z['itemInfos']['id'] not in dicts[z['authorInfos']['uniqueId']]['in']:
                    dicts[z['authorInfos']['uniqueId']]['in'].append(z['itemInfos']['id'])
        elif 'author' in z.keys():
            if z['author']['uniqueId'] not in dicts.keys():
                dicts[z['author']['uniqueId']] = {'in':[z['id']],'out':[],'vc':int(z['authorStats']['videoCount']),'fc':int(z['authorStats']['followerCount'])}
            else:
                if z['id'] not in dicts[z['author']['uniqueId']]['in']:
                    dicts[z['author']['uniqueId']]['in'].append(z['id'])
        i += 1
        if i > 1500:
            break

hts=[]
with open('D:\\Work\\kusuri\\usernameswithhts_foodtiktok.csv','w',newline='\n',encoding='utf-8')as fin:
    writer = csv.writer(fin)
    writer.writerow(['Username','Within Hashtag','Among all hashtags','video count','follower count'])
    for fname in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if not fname.endswith('.json'):
            continue
        if not (fname.lower().startswith('hashtag')):
            continue
        ht=fname.split('_')[1]
        if ht in hts:
            continue
        hts.append(ht)

        with open('D:\\Work\\Tool\\tiktok\\TikToks\\'+fname, 'r', encoding='utf-8', newline='\n') as filename_input:
            i=0
            for line in filename_input:
                z = json.loads(line)
                i += 1
                if i > 1500:
                    break
                if 'authorInfos' in z.keys():
                    if z['authorInfos']['uniqueId'] in dicts.keys():
                        if z['itemInfos']['id'] not in dicts[z['authorInfos']['uniqueId']]['out']:
                            dicts[z['authorInfos']['uniqueId']]['out'].append(z['itemInfos']['id'])
                elif 'author' in z.keys():
                    if z['author']['uniqueId'] in dicts.keys():
                        if z['id'] not in dicts[z['author']['uniqueId']]['out']:
                            dicts[z['author']['uniqueId']]['out'].append(z['id'])

    for u in dicts.keys():
        writer.writerow([u,len(dicts[u]['in']),len(dicts[u]['out']),dicts[u]['vc'],dicts[u]['fc']])